package com.jdd.modules.rabbitMq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jdd.common.constant.JddConstant;
import com.jdd.common.constant.StuckCarRabbitMqConstant;
import com.jdd.common.entity.RabbitMqMessage;
import com.jdd.modules.parking.service.IZombieCarService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class ParkCloudStuckCarListener {



    @Resource
    private IZombieCarService iZombieCarService;

    @RabbitListener(queues = "${park.stuck.car.queue}")
    @RabbitHandler
    public void receiveMessage(@Payload String body, @Headers Map<String, Object> headers, Channel channel) throws IOException {
        log.info("接收到云端消息{}", body);
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        RabbitMqMessage rabbitMqMessage = null;

        try {
            rabbitMqMessage = JSON.parseObject(body, RabbitMqMessage.class);
        } catch (Exception e) {
            log.error("消息转换异常", e.getMessage());
            //非法消息，抛弃此条消息
            channel.basicNack(deliveryTag, false, false);
            return;
        }

        if (null == rabbitMqMessage || StringUtils.isBlank(rabbitMqMessage.getMessageType()) || StringUtils.isBlank(rabbitMqMessage.getMessageBody())) {
            log.info("空消息{}", JSON.toJSONString(rabbitMqMessage));
            //空消息，抛弃此条消息
            channel.basicNack(deliveryTag, false, false);
            return;
        }

        //处理消息
        try {
            process(rabbitMqMessage, deliveryTag, channel);
            //确认消息
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            log.error("处理消息异常：{}", e.getMessage());
            //抛弃此条消息
            channel.basicNack(deliveryTag, false, false);
        }

    }

    /**
     * @return void
     * @Description 业务不同异常处理(消息确认 、 消息返回队列 、 消息抛弃)
     * @Author wanglei
     * @Date 2021-06-02 17:28
     * @Param [rabbitMqMessage, deliveryTag, channel]
     * @version 1.0
     **/
    private void process(RabbitMqMessage rabbitMqMessage, Long deliveryTag, Channel channel) throws IOException {
        if (rabbitMqMessage.getMessageType().equals(StuckCarRabbitMqConstant.PARK_STUCK_CAR_MQ_MESSAGE_TYPE_OUT_ONE)) {
            JSONObject jsonObject = JSON.parseObject(rabbitMqMessage.getMessageBody());
            String plateNo = jsonObject.getString("plateNo");
            String armId = jsonObject.getString("armId");
            log.info("滞留车手动出场，车牌号：{},通道id:{}", plateNo, armId);
            iZombieCarService.zombieToOut(plateNo, armId);
        }
    }
}
